package com.kly.cashmall.utils.data;

import android.text.TextUtils;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author zxp
 * @since 27/10/21
 */
public class DateUtil {
    public static final String EARLY_TIME = "1970-01-01 08:00:00";
    public static final String FUTURE_TIME = "2100-01-01 00:00:00";
    public static Long EARLY_TIME_LONG = 0L;
    public static Long FUTURE_TIME_LONG = 4102416000000L;

    public DateUtil() {
    }

    public static String parseDate(long time, String formatStr) {
        if (time <= 0L) {
            return "";
        } else {
            SimpleDateFormat format = new SimpleDateFormat(formatStr);
            Date date = new Date(time);
            return format.format(date);
        }
    }

    public static Long strToDateLong(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date date = formatter.parse(strDate, pos);
        return date.getTime();
    }

    public static Long strToDateLong(String strDate, String dateFormat) {
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        ParsePosition pos = new ParsePosition(0);
        Date date = formatter.parse(strDate, pos);
        return date.getTime();
    }

    public static Date strToDate(String strDate, String formatStr) {
        SimpleDateFormat formatter = new SimpleDateFormat(formatStr);
        ParsePosition pos = new ParsePosition(0);
        Date date = formatter.parse(strDate, pos);
        return date;
    }

    public static Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date date = formatter.parse(strDate, pos);
        return date;
    }

    public static String parseDate(long time) {
        return parseDate(time, "yyyy-MM-dd HH:mm:ss");
    }

    public static String parseTime(int time) {
        int s = time % 60;
        int m = time / 60;
        StringBuilder builder = new StringBuilder();
        if (m < 10) {
            builder.append("0").append(m);
        } else {
            builder.append(m);
        }

        builder.append(":");
        if (s < 10) {
            builder.append("0").append(s);
        } else {
            builder.append(s);
        }

        return builder.toString();
    }

    public static int dateToAge(String strDate) {
        if (TextUtils.isEmpty(strDate)) {
            return 0;
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            ParsePosition pos = new ParsePosition(0);
            Date date = formatter.parse(strDate, pos);
            return getAge(date);
        }
    }

    public static int getAge(Date birthDay) {
        Calendar cal = Calendar.getInstance();
        if (birthDay != null && !cal.before(birthDay)) {
            int yearNow = cal.get(1);
            int monthNow = cal.get(2);
            int dayOfMonthNow = cal.get(5);
            cal.setTime(birthDay);
            int yearBirth = cal.get(1);
            int monthBirth = cal.get(2);
            int dayOfMonthBirth = cal.get(5);
            int age = yearNow - yearBirth;
            if (monthNow <= monthBirth) {
                if (monthNow == monthBirth) {
                    if (dayOfMonthNow < dayOfMonthBirth) {
                        --age;
                    }
                } else {
                    --age;
                }
            }

            return age;
        } else {
            return 0;
        }
    }

    public static boolean isToday(String date) {
        String today = parseDate(System.currentTimeMillis());
        return TextUtils.substring(today, 0, 10).equals(TextUtils.substring(date, 0, 10));
    }

}
